/*
 * @Date: 2020-09-14 15:52:38
 * @LastEditors: 小枫
 * @LastEditTime: 2020-09-21 13:17:54
 * @FilePath: \feng-blog\app\service\Tag.ts
 */
import { Service } from 'egg';


interface Tag {
  tag: string;
  photoUrl: string;
}
interface FinTag extends Tag {
  tid: number;
}
/**
 * Tags Service
 */
export default class TagService extends Service {

  /**
   * 获取所有的分类
   */
  public async getAllTags() {
    const { app } = this;
    const tags = app.mysql.select('tags', {});
    return tags;
  }

  /**
   * 新增分类
   * @param tagObj -分类对象
   */
  public async insertTag(tagObj: Tag) {
    const { app } = this;
    const result = await app.mysql.insert('tags', tagObj);
    if (result.affectedRows === 1) {
      return result;
    }
  }

  /**
   * 根据分类名获取分类
   * @param tag -分类名
   */
  public async getTagByName(tag: string) {
    const { app } = this;
    const result = await app.mysql.select('tags', {
      where: {
        tag,
      },
    });
    return result;
  }

  /**
   * 修改分类信息
   * @param tag -分类
   */
  public async updateTag(tag: FinTag) {
    const { app } = this;
    const { tid } = tag;
    const result = await app.mysql.update('tags', tag, {
      where: {
        tid,
      },
    });
    if (result.affectedRows === 1) {
      return result;
    }
  }

  /**
   * 删除分类
   * @param tid -分类唯一标识
   */
  public async deleteTag(tid: number) {
    const { app } = this;
    const result = await app.mysql.delete('tags', {
      tid,
    });
    return result;
  }
}
